package com.grantz.function.bbs.core.controller;

import java.util.Optional;

import javax.annotation.Resource;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.grantz.function.bbs.core.entity.CommentChild;
import com.grantz.function.bbs.core.inter.CommentChildService;
import com.grantz.model.bbs.CommentChildModel;
import com.grantz.model.bbs.CommentChildSearch;
import com.grantz.models.base.element.page.ElementPage;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 文件名称： com.grantz.function.bbs.core.controller.CommentChildController.java</br>
 * 初始作者： WenBin</br>
 * 创建日期： 2020年2月13日</br>
 * 功能说明： 评论子评论 <br/>
 * =================================================<br/>
 * 修改记录：<br/>
 * 修改作者        日期       修改内容<br/>
 * ================================================<br/>
 *  Copyright (c) 2010-2011 .All rights reserved.<br/>
 */
@RestController
@RequestMapping("/api/v1/commentchilds")
@Api(value="评论子评论记录",tags={"评论子评论记录接口"})
public class CommentChildController {
	@Resource
	private CommentChildService  commentChildService;

	// 获取群组用户
	@ApiOperation(value = "获取分页信息")
	@GetMapping
	public ResponseEntity<ElementPage<CommentChildModel>>  get(CommentChildSearch query) throws Exception{
		ElementPage<CommentChildModel> element =  commentChildService.queryElementPage(query);
		return new ResponseEntity<ElementPage<CommentChildModel>>(element ,HttpStatus.CREATED);
	}

	@ApiOperation(value = "保存")
	@PutMapping
	public ResponseEntity<?>  save(@RequestBody CommentChildModel model){
		commentChildService.save(pop(new CommentChild() ,model));
		return new ResponseEntity<>(HttpStatus.CREATED);
	}

	private CommentChild pop(CommentChild entity ,CommentChildModel model) {
		return entity;
	}

	@ApiOperation(value = "更新")
	@PostMapping
	public ResponseEntity<?>  update(@RequestBody CommentChildModel model){
		Optional<CommentChild> option = commentChildService.findOneById(model.getId());
		if(option.isPresent()) {
			commentChildService.save(pop(option.get() , model));
		}
		return new ResponseEntity<>(HttpStatus.CREATED);
	}


}
